package com.liangzdemo.book;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.liangzdemo.R;

import java.util.ArrayList;
import java.util.List;

public class BookManagerActivity extends Activity {

    private static final String TAG = "BookMangerActvity";

    private ListView mylistview;
    private BaseAdapter adapter;//要实现的类
    private List<Book> list= new ArrayList<Book>();
    private TextView tv1;//item.xml里的TextView：Textviewname
    private TextView tv2;

    private ServiceConnection mConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            IBookManager bookManager = IBookManager.Stub.asInterface(service);
            try {
                list = bookManager.getBookList();
                adapter.notifyDataSetChanged();
                Log.d(TAG,"list.get(0).bookName=" + list.get(0).bookName + ",list.get(0).bookId=" + list.get(0).bookId + "list.get(1).bookName=" + list.get(1).bookName + ",list.get(1).bookId=" + list.get(1).bookId);

            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate");
        setContentView(R.layout.content_book_manager);
        // android 5.0之后所有的服务必须显示调用，不能使用隐私调用
        //Intent intent = new Intent(this, BookManagerService.class);
        Intent intent = new Intent();
        intent.setAction("com.liangz.aidl.booksvc");
        intent.setPackage(this.getPackageName());

        bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
        initUI();

    }

    @Nullable
    @Override
    public View onCreateView(String name, Context context, AttributeSet attrs) {
        return super.onCreateView(name, context, attrs);
    }

    @Override
    protected void onDestroy() {
        unbindService(mConnection);
        super.onDestroy();
    }

    private void initUI() {
        adapter = new BaseAdapter() {
            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                Log.d(TAG,"getCount_list.size()=" + list.size());
                return list.size();//数目
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                LayoutInflater inflater = BookManagerActivity.this.getLayoutInflater();
                View view;

                if (convertView==null) {
                    //因为getView()返回的对象，adapter会自动赋给ListView
                    view = inflater.inflate(R.layout.item_book, null);
                }else{
                    view=convertView;
                    Log.i("info","有缓存，不需要重新生成"+position);
                }
                tv1 = (TextView) view.findViewById(R.id.Textviewname);//找到Textviewname

                Log.d(TAG,"getView_list.size()=" + list.size());

                tv1.setText(Integer.toString(list.get(position).bookId));//设置参数

                tv2 = (TextView) view.findViewById(R.id.Textviewage);//找到Textviewage
                tv2.setText(list.get(position).bookName);//设置参数

                return view;
            }
            @Override
            public long getItemId(int position) {//取在列表中与指定索引对应的行id
                return 0;
            }
            @Override
            public Object getItem(int position) {//获取数据集中与指定索引对应的数据项
                return null;
            }
        };

        mylistview = (ListView)findViewById(R.id.list_view);
        mylistview.setAdapter(adapter);
    }
}
